package com.zc.im.util.login;

import com.zc.im.model.user.ZcUser;
import com.zc.im.service.user.UserService;
import com.zc.im.util.ImGolbalTypeContext;
import com.zc.im.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/7/22.
 */
@Component
public class UserSession {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;
    /**
     * 获取session
     * @return
     */
    public ZcUser getSession(HttpServletRequest request){
        ZcUser zcUser = null;
        if(request == null){
            return zcUser;
        }
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        boolean hasKey = redisTemplate.hasKey(request.getAttribute("PHPSESSID"));
        if (hasKey) {
            String str = operations.get(request.getAttribute("PHPSESSID"));
        }
        HttpSession session = request.getSession();
        String uid = (String) session.getAttribute(ImGolbalTypeContext.IM_SESSEION_UID);
        String userType = (String) session.getAttribute(ImGolbalTypeContext.IM_SESSEION_USERTYPE);
        String fullName = (String) session.getAttribute(ImGolbalTypeContext.IM_SESSEION_FULLNAME);
        String phone = (String) session.getAttribute(ImGolbalTypeContext.IM_SESSEION_PHONE);

        if(StringUtils.isEmpty(uid) || StringUtils.isEmpty(userType) ||
                StringUtils.isEmpty(fullName) || StringUtils.isEmpty(phone)){
            return zcUser;
        }
        zcUser = userService.findUser(Integer.parseInt(uid));
        return zcUser;
    }



}
